// pages/mys/guo/crop/one/index.js
const app = getApp(); // 获取应用实例
var Utils = require('../../../../../utils/crypt.js'); //加密解密

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0, guke: '', weight: '', price: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options); 
    var that = this;
    var id = options.id;
    var guke = options.guke;
    var weight = options.weight;
    var price = options.price;
    weight = weight!=0 ? weight : '';
    price = price!=0 ? price : '';
    that.setData({
      id: id, guke: guke, weight: weight, price: price,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //返回
  toBack: function () {
    console.log('返回');
    wx.showToast({
      title: '即将返回。。', icon: 'none'
    })
    wx.redirectTo({
      url: '/pages/mys/guo/crop/index'
    })
  },
  //顾客
  inputGuke: function (e) {
    console.log(e);
    var that = this;
    var guke = e.detail.value;
    that.setData({guke:guke});
  },
  //重量
  inputWeight: function (e) {
    console.log(e);
    var that = this;
    var weight = e.detail.value;
    that.setData({weight:weight});
  },
  //价格
  inputPrice: function (e) {
    console.log(e);
    var that = this;
    var price = e.detail.value;
    that.setData({price:price});
  },
  //保存提交
  formSave: function () {
    var that = this;
    var uwx = app.globalData.usertest;
    var userType = app.globalData.userType;
    var id = that.data.id;
    var guke = that.data.guke;
    var weight = that.data.weight;
    var price = that.data.price;
    var formData = {
      wx: uwx, userType: userType, 
      id: id, guke: guke, weight: weight, price: price,
    };
    wx.showToast({title: '开始保存..', icon: 'none'}); 
    //请求数据
    wx.request({
      url: app.globalData.domain+'/my/guo/crop/modify',
      method: "POST",
      header: {'content-type': 'application/x-www-form-urlencoded'},
      data: formData,
      success: function (res) {
        wx.hideLoading();
        if (res.data.code!=0) {
          wx.showToast({
            title: res.data.msg, icon: 'none',
          }); return
        } 
        wx.showToast({
          title: '保存成功..', icon: 'none',
        }); 
        setTimeout(function(){
          wx.redirectTo({
            url: '/pages/mys/guo/crop/index'
          });
        },2000);
      }
    });
  },
})